<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class transaksi extends CI_Controller
{
    public function __construct()
    {
        parent::__construct();

        $this->load->library('auth');
        $this->load->library('base');
        $this->load->library('ajaxresponse');
        $this->load->library('datatables');
    }

    function index(){
        $this->auth->is_logged_in('form/login');
        
    }

    function viabank(){
        $this->auth->is_logged_in('form/login');
        $this->base->head_html('Persiapan Data Transaksi Keuangan');
        $this->load->model('crud/m_info_usaha');
        $data["info_usaha"] = $this->m_info_usaha->get()->result();
        $this->load->view('pengurus/br-04/viabank',$data);
    }
    
    function viakasir(){
        $this->auth->is_logged_in('form/login');
        $this->base->head_html('Persiapan Data Transaksi Keuangan');
        $this->load->model('crud/m_jenis_data_transaksi');
        $this->load->model('crud/m_jenis_transaksi');
        $data["Data_transaksi"] = $this->m_jenis_data_transaksi->get();
        $data["Transaksi"] = $this->m_jenis_transaksi->get();
        $this->load->view('pengurus/br-04/viakasir',$data);
    }

    function add_bukti(){
        $this->auth->is_logged_in('form/login');
        $this->response = new ajaxresponse();
        $this->load->model('crud/m_bukti_transaksi');
        $this->load->model('crud/m_pinjaman');
        
        $nomer = $this->input->post('BKK_BKB',true);
        $cek = $this->m_bukti_transaksi->get_transaksi($nomer);
        if ($cek->num_rows() == 0){
            $id = $this->m_bukti_transaksi->insert_bukti();
            $this->response->id_bukti = $id;
            //$this->m_pinjaman->approve2($this->input->post('ID_PINJAMAN', TRUE),$id);
        }else{
            $result = $cek->row();
            $this->response->id_bukti = $result->ID_BUKTI_TRANSAKSI;
            $this->response->data = $cek->result();
        }
        echo json_encode($this->response);
    }

    function add_perkiraan(){
        $this->auth->is_logged_in('form/login');
        $this->response = new ajaxresponse();
        $this->load->model('crud/m_bukti_transaksi');
        $this->response->id_perkiraan = $this->m_bukti_transaksi->insert_perkiraan();
        echo json_encode($this->response);
    }

    function delete_detail_perkiraan($id){
        $this->auth->is_logged_in('form/login');
        $this->response = new ajaxresponse();
        $this->load->model('crud/m_bukti_transaksi');
        
	$this->response->id_perkiraan = $this->m_bukti_transaksi->delete_detail_perkiraan($id);
        echo json_encode($this->response);
    }

    function delete_bukti_transaksi($id_bukti){
        $this->auth->is_logged_in('form/login');
        $this->response = new ajaxresponse();
        $this->load->model('crud/m_bukti_transaksi');
        $this->m_bukti_transaksi->delete_detail($id_bukti);
	$this->response->id_perkiraan = $this->m_bukti_transaksi->delete($id_bukti);
        echo json_encode($this->response);
    }

    function get_detail_bukti(){
       $this->auth->is_logged_in('form/login');
        $this->response = new ajaxresponse();
        $this->load->model('crud/m_bukti_transaksi');
        $result = $this->m_bukti_transaksi->get_transaksi($this->input->post('BKK_BKB',true))->row();
        $this->response->data = $this->m_bukti_transaksi->get_detail_bukti($result->ID_BUKTI_TRANSAKSI)->result();
        echo json_encode($this->response);
    }

    function add_viakasir(){
        $this->auth->is_logged_in('form/login');
        $this->response = new ajaxresponse();
        $this->load->model('crud/m_bukti_transaksi');
        $this->load->model('crud/m_pinjaman');
        $jenis_trans = $this->input->post('DATA_TRANSAKSI',TRUE);
        $id = $this->input->post('ID_PINJAMAN',true);
        $nomer = $this->input->post('BKK_BKB',true);
        $cek = $this->m_bukti_transaksi->get_transaksi($nomer)->num_rows();
        if ($cek==0){
            $id_bukti = $this->m_bukti_transaksi->insert();
            $this->response->data = $id_bukti;
            $result = $this->m_pinjaman->approve2($id,$id_bukti);
            $this->response->data = $result;
        }else $this->response->data = -1;
        
        echo json_encode($this->response);
    }

    function update_viakasir(){
        $this->auth->is_logged_in('form/login');
        $this->response = new ajaxresponse();
        $this->load->model('crud/m_bukti_transaksi');
        $this->m_bukti_transaksi->update_viakasir();
        echo json_encode($this->response);
    }

    function add_viabank(){
        $this->auth->is_logged_in('form/login');
        $this->response = new ajaxresponse();
        $this->load->model('crud/m_bukti_transaksi');
        $this->load->model('crud/m_pinjaman');
        $this->load->model('crud/m_anggota');
        $this->load->model('crud/m_bukti_transaksi');
        $this->load->model('crud/m_info_usaha');

        $this->response->jumlahpenerimaan = '';
        $this->response->bukti = '';
        $this->response->data_potongan = '';
        $this->response->data_simpanan = '';

        $date = $this->input->post('TGL_BPB',true);
        $penerimaan = $this->input->post('JUMLAH_PENERIMAAN',true);
        $nomer_bukti = $this->input->post('BPB',true);
        $kode_unit = $this->input->post('KODE_UNIT',true);

        
        $info_usaha = $this->m_info_usaha->get()->result();
        
        $where = 1;
        $simpanan_wajib = TRUE;

        /*
        foreach($info_usaha as $idcekbox){
            if ($idcekbox->URAIAN_USAHA=='Simpanan Wajib' && $this->input->post('13',true)!=null){
                $simpanan_wajib = true;
            }else 
            if ($this->input->post($idcekbox->KODE_USAHA,true)!=null){
                $where ++;
            }
        }
        */
        
        $this->response->usaha = $where;

        ////PENGECEKAN JUMLAH DITERIMA DAN JUMLAH POTONGAN////
        // SIMPANAN WAJIB ANGGOTA DIHITUNG DENGAN JUMLAH POTONGAN PADA PINJAMAN
        // JIKA HASIL TOTAL TIDAK SAMA DENGAN PENERIMAAN VIA BANK
        // SISTEM AKAN MENOLAK MELAKUKAN TRANSAKSI INI //
        
        // hitung jumlah simpanan wajib semua anggota
        $total_simpanan = 0;
        $dari_simpanan = $this->m_anggota->get_simpanan($date)->result();
        foreach($dari_simpanan as $simpanan){
            $total_simpanan += $simpanan->SIMPANAN_WAJIB;
        }
        $this->response->simpanan = $total_simpanan;

        // hitung jumlah semua potongan
        $dari_pinjaman = $this->m_pinjaman->sum_potongan()->row();
        $total_potongan = $dari_pinjaman->total;
        $this->response->potongan = $total_potongan;
        
        $margin = (abs($penerimaan-($total_potongan+$total_simpanan)));
        $this->response->margin = $margin;
        $this->response->total = $total_potongan+$total_simpanan;

        if ($margin==0)
        {
           $cek_bukti = $this->m_bukti_transaksi->get_transaksi($nomer_bukti)->num_rows();
            if ($cek_bukti==0){
               $id_bukti = $this->m_bukti_transaksi->insert_viabank();
                $this->response->data_simpanan = $this->m_anggota->generate_realisasi($date,$id_bukti);
                $this->response->data_potongan = $this->m_pinjaman->generate_realisasi($date,$id_bukti);
            }else $this->response->bukti = "Nomer bukti transaksi sudah terdaftar.";
            
        }else $this->response->jumlahpenerimaan = "Jumlah penerimaan dan jumlah potongan tidak sama.";

        echo json_encode($this->response);
        
    }
    
    function buktijurnalumum(){
        
    }
    
    function neracasaldo(){
        
    }

}